package util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ImportFile {
    //读取文件的每一行，list中每一个都是一行数据（String）
    public static List readFile(String filePath){
        File tsv = new File(filePath);  // 文件路径
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(tsv));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String line = "";
        String everyLine = "";

        List<String> allString = null;

        try {
            allString = new ArrayList<>();
            while ((line = br.readLine()) != null)  //读取到的内容给line变量
            {
                everyLine = line;
                //System.out.println(everyLine);
                allString.add(everyLine);
            }
            //System.out.println("csv表格中所有行数：" + allString.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return allString;
    }


    //提取某一列的内容
    public static String[] extract(List list,String symbol,int column){
        //list:提取的数据（每一个元素是一行数据，未被分割）
        int listLength = list.size();
        String[] data = new String[listLength];
        int column_length = list.get(0).toString().split(symbol).length;
        String[] lineData = new String[column_length];

        for (int i= 0 ;i<listLength;i++){
            lineData = list.get(i).toString().split(symbol);
            data[i] = lineData[column-1];
        }

        return data;
    }
}
